package com.lanlian.cayman.kline.utils;

import com.example.bean.CMinute;
import com.example.bean.FenshiDataResponse;
import com.example.bean.FenshiParam;
import com.example.bean.StickData;
import com.lanlian.cayman.utils.NumberManager;
import com.lanlian.cayman.utils.TimeUtil;


import java.util.ArrayList;


public class MyDataParse {
    /**
     * yyyy-MM-dd
     */
    String date;
    public MyDataParse()
    {

    }

    /**均价|最新价\成交量|时间+
     * 0.000000|67.680000|685|16:03:59.287
     */
    public static FenshiDataResponse parseFenShi(String data)
    {

        FenshiDataResponse fd=new FenshiDataResponse();
        FenshiParam fp=new FenshiParam();
        fp.setDuration("9:30-11:30|13:00-15:00");

        fp.setLength(14400);
        fp.setUntil(1478846640);


        String last=null;

        ArrayList<CMinute> list=new ArrayList<>();
        CMinute cm=null;
        String[] item=null;
        String time=null;
        String rage=null;
        try
        {

            String[] line=data.split("\\+");


            for(int i=0;i<line.length;i++)
            {
                  cm=new CMinute();
                  item=line[i].split("\\|");
                  if(item.length<4)
                  {
                      continue;
                  }
                  if(i==0)
                  {
                      last=item[0];
                      fp.setLast(Double.parseDouble(item[0]));
                  }
                  if(!NumberManager.isDouble(item[0]))
                  {
                      continue;
                  }
                if(!NumberManager.isDouble(item[1]))
                {
                    continue;
                }
                if(!NumberManager.isDouble(item[2]))
                {
                    continue;
                }
                if(!NumberManager.isDouble(item[3]))
                {
                    continue;
                }

                  if(i==0)
                  {
                      time=item[4];
                      cm.setTime(getTime2(time));
                      cm.setAverage(Double.parseDouble(item[1]));
                      cm.setPrice(Double.parseDouble(item[2]));
                      cm.setCount(Long.parseLong(item[3]));
                      fp.setLast(Double.parseDouble(item[0]));
                      last=item[0];

                      rage= NumberManager.getSubString(item[0],last,8);
                      cm.setChange(NumberManager.getString(rage,"1",8));
                      rage=NumberManager.getDivString(rage,last,8);
                      rage=NumberManager.getString(rage,"100",2);
                      cm.setRate(Double.parseDouble(rage));
                      cm.setChg(rage+"%");
                      //cm.setAverage(67);
                  }
                  else
                  {
                      time=item[3];
                      cm.setTime(getTime2(time));
                      cm.setAverage(Double.parseDouble(item[0]));
                      cm.setPrice(Double.parseDouble(item[1]));
                      cm.setCount(Long.parseLong(item[2]));

                      rage= NumberManager.getSubString(item[1],last,8);
                      cm.setChange(NumberManager.getString(rage,"1",8));
                      rage=NumberManager.getDivString(rage,last,8);
                      rage=NumberManager.getString(rage,"100",2);
                      cm.setRate(Double.parseDouble(rage));
                      cm.setChg(rage+"%");

                      //cm.setAverage(67);

                  }
                  list.add(cm);


            }



        }
        catch (Exception e)
        {
            e.printStackTrace();

        }
        fd.setData(list);
        fd.setParam(fp);
        return fd;
    }
    /**
     * 收盘价|最高价|最新价|最低价|开盘|成交量|时间+
     * @param min  周期分钟数
     */
    public static StickData parse2(String data,int min)
    {
        StickData sd=new StickData();
        try
        {

                String[] item=data.split("\\|");
              sd.setClose(Float.parseFloat(item[0]));

                sd.setHigh(Float.parseFloat(item[1]));
            sd.setLast(Float.parseFloat(item[2]));
                sd.setLow(Float.parseFloat(item[3]));


                sd.setOpen(Float.parseFloat(item[4]));


                sd.setCount(Double.parseDouble(item[5]));
                //long time=getTime(item[6]);
                //long yu=time%(min*60);
                sd.setTime(getTime(item[6]));

           // sd.setTime(Long.parseLong(item[6]));

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return sd;
    }
    /**
     * 最高价|最低价|最新价|开盘价|收盘价|成交量|时间+
     */
    public static ArrayList<StickData> parse(String data)
    {
        ArrayList<StickData> list=new ArrayList<>();
       try
       {
            String[] line=data.split("\\+");
            for(int i=0;i<line.length;i++)
            {

                String[] item=line[i].split("\\|");
                StickData sd=new StickData();
                sd.setHigh(Float.parseFloat(item[0]));
                sd.setLow(Float.parseFloat(item[1]));
                sd.setLast(Float.parseFloat(item[2]));
                sd.setClose(Float.parseFloat(item[4]));
                sd.setOpen(Float.parseFloat(item[3]));


                sd.setCount(Double.parseDouble(item[5]));

                sd.setTime(getTime(item[6]));
                list.add(sd);
            }
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }
        return list;
    }


    private static long getTime(String time)
    {
       long l1=Long.parseLong(TimeUtil.Date2TimeStamp(time,"yyyyMMddHHmmss"));
//        long l2=l1%(60);
//        l1=l1-l2+60;
       return  l1;
       // return Long.parseLong(TimeUtil.Date2TimeStamp(time,"yyyyMMddHHmmss"));
    }
    private static long getTime2(String time)
    {
        String tset=time.substring(8,10);
       if(tset.contains("21"))
       {
           int a=1;
       }
         time=time.substring(0,14);
        long l1=Long.parseLong(TimeUtil.Date2TimeStamp(time,"yyyyMMddHHmmss"));
//        long l2=l1%(60);
//        l1=l1-l2+60;
        return  l1;
       // return Long.parseLong(TimeUtil.Date2TimeStamp(time,"yyyyMMddHHmmss"));
    }


}
